package qdu.szmt.testfrag;
/************************************
 * 
 1.Mymain  ����activity����Ӧ�Ĳ����ļ�mainlayout.xml
 2.��Mymain�н���̬ע��fragment��fragment��Myfragment.java��Ӧ�Ĳ���Ϊfg_content.xml
 3.tab_menu_*.xml�ļ�����Ϊselector����ǩ����mainlayout.xml���ʵ�ֽ��涯̬�仯���ļ�
 4.����*.png����ͼƬ�ļ�������selector�ļ��еĴ���������
 5.color/strings/styles.xml �����ṩֵ����Դ�ļ�
 */
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.view.View;
public class Mymain extends Activity implements View.OnClickListener{
	 //UI Object
    private TextView txt_topbar;
    private TextView txt_channel;
    private TextView txt_message;
    private TextView txt_better;
    private TextView txt_setting;
    private FrameLayout ly_content;

    //Fragment Object
    private Myfragment fg1,fg2,fg3,fg4;
    private FragmentManager fManager;
    /**
     * 1) fragmentmanager
     * getFragmentManager()
     *
     * 2) transaction
     * getFragmentManager().beginTransaction()
     * 碎片业务对象可以对碎片进行直接处理，比如add  replace / hide  show
     *
     * 3) 创建fragment对象
     *
     * 4) 利用业务员对象的add、replace、hide 、show来决定当前要如何显示这些碎片对象
     *
     * 5）调用commit函数，使设置生效
     */

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.mainlayout);
		txt_topbar = (TextView) findViewById(R.id.txt_topbar);
        txt_channel = (TextView) findViewById(R.id.txt_channel);
        txt_message = (TextView) findViewById(R.id.txt_message);
        txt_better = (TextView) findViewById(R.id.txt_better);
        txt_setting = (TextView) findViewById(R.id.txt_setting);
        ly_content = (FrameLayout) findViewById(R.id.ly_content);
		fManager = getFragmentManager();
		txt_channel.setOnClickListener(this);
	    txt_message.setOnClickListener(this);
	    txt_better.setOnClickListener(this);
	    txt_setting.setOnClickListener(this);
	    txt_channel.setSelected(true);
	}
	@Override
	public void onClick(View v) {
		FragmentTransaction fTransaction = fManager.beginTransaction();
        hideAllFragment(fTransaction);
        switch (v.getId()){
            case R.id.txt_channel:
                setSelected();
                txt_channel.setSelected(true);
                if(fg1 == null){
                    fg1 = new Myfragment("��һ��Fragment");
                    fTransaction.add(R.id.ly_content,fg1);
                }else{
                    fTransaction.show(fg1);
                }
                break;
            case R.id.txt_message:
                setSelected();
                txt_message.setSelected(true);
                if(fg2 == null){
                    fg2 = new Myfragment("�ڶ���Fragment");
                    fTransaction.add(R.id.ly_content,fg2);
                }else{
                    fTransaction.show(fg2);
                }
                break;
            case R.id.txt_better:
                setSelected();
                txt_better.setSelected(true);
                if(fg3 == null){
                    fg3 = new Myfragment("������Fragment");
                    fTransaction.add(R.id.ly_content,fg3);
                }else{
                    fTransaction.show(fg3);
                }
                break;
            case R.id.txt_setting:
                setSelected();
                txt_setting.setSelected(true);
                if(fg4 == null){
                    fg4 = new Myfragment("���ĸ�Fragment");
                    fTransaction.add(R.id.ly_content,fg4);
                }else{
                    fTransaction.show(fg4);
                }
                break;
        }
        fTransaction.commit();
	}
    //���������ı���ѡ��״̬
    private void setSelected(){
        txt_channel.setSelected(false);
        txt_message.setSelected(false);
        txt_better.setSelected(false);
        txt_setting.setSelected(false);
    }

    //��������Fragment
    private void hideAllFragment(FragmentTransaction fragmentTransaction){
        if(fg1 != null)fragmentTransaction.hide(fg1);
        if(fg2 != null)fragmentTransaction.hide(fg2);
        if(fg3 != null)fragmentTransaction.hide(fg3);
        if(fg4 != null)fragmentTransaction.hide(fg4);
    }
}
